package ecom.beans;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "purchase")
@NamedQueries({
    @NamedQuery(name = "PurchaseBean.findAll", query = "SELECT p FROM PurchaseBean p"),
    @NamedQuery(name = "PurchaseBean.findByPK", query = "SELECT p FROM PurchaseBean p WHERE p.purchaseBeanPK.iban = :iban"
    + " and  p.purchaseBeanPK.idProductStore = :idProductStore"
    + " and p.purchaseBeanPK.idProduct = :idProduct"
    + " and  p.purchaseBeanPK.dateAchat = :dateAchat"),
    @NamedQuery(name = "PurchaseBean.lastSales", query = "SELECT p FROM PurchaseBean p")
})
public class PurchaseBean implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected PurchaseBeanPK purchaseBeanPK;
    @Basic(optional = false)
    @Column(name = "quantity", nullable = false)
    private Integer quantity;
    @Basic(optional = false)
    @Column(name = "price", nullable = false)
    private Double price;
    @JoinColumn(name = "iban", referencedColumnName = "iban", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private AccountBean accountBean;
    @JoinColumn(name = "id_product", referencedColumnName = "id_product", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private ProductBean productBean;
    @JoinColumn(name = "id_product_store", referencedColumnName = "id_product_store", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private ProductstoreBean productstoreBean;

    public PurchaseBean() {
    }

    public PurchaseBean(PurchaseBeanPK purchaseBeanPK) {
        this.purchaseBeanPK = purchaseBeanPK;
    }

    public PurchaseBean(AccountBean accountBean, ProductstoreBean productstoreBean, ProductBean productBean, Integer quantity, Double price) {
        purchaseBeanPK = new PurchaseBeanPK(accountBean.getIban(), productstoreBean.getIdProductStore(), productBean.getIdProduct(), new Date());
        this.accountBean = accountBean;
        this.productBean = productBean;
        this.productstoreBean = productstoreBean;
        this.quantity = quantity;
        this.price = price;
    }

    public PurchaseBean(String iban, Long idProductStore, Long idProduct, Date dateAchat) {
        this.purchaseBeanPK = new PurchaseBeanPK(iban, idProductStore, idProduct, dateAchat);
    }

    public PurchaseBeanPK getPurchaseBeanPK() {
        return purchaseBeanPK;
    }

    public void setPurchaseBeanPK(PurchaseBeanPK purchaseBeanPK) {
        this.purchaseBeanPK = purchaseBeanPK;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public AccountBean getAccountBean() {
        return accountBean;
    }

    public void setAccountBean(AccountBean accountBean) {
        this.accountBean = accountBean;
    }

    public ProductBean getProductBean() {
        return productBean;
    }

    public void setProductBean(ProductBean productBean) {
        this.productBean = productBean;
    }

    public ProductstoreBean getProductstoreBean() {
        return productstoreBean;
    }

    public void setProductstoreBean(ProductstoreBean productstoreBean) {
        this.productstoreBean = productstoreBean;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (purchaseBeanPK != null ? purchaseBeanPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PurchaseBean)) {
            return false;
        }
        PurchaseBean other = (PurchaseBean) object;
        if ((this.purchaseBeanPK == null && other.purchaseBeanPK != null) || (this.purchaseBeanPK != null && !this.purchaseBeanPK.equals(other.purchaseBeanPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "[idProductstore=" + purchaseBeanPK.getIdProductStore() + ",idProduct=" + purchaseBeanPK.getIdProduct()
                + ",iban=" + purchaseBeanPK.getIban() + ",dateAchat=" + purchaseBeanPK.getDateAchat()
                + ",quantity=" + quantity + ",price=" + price + "]";
    }
}
